/**
 * 
 */
package controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import model.EErrorPopUp;
import model.cnf.CNF;
import model.cnf.FileCNF;
import model.preprocessor.PreProcessorFactory;
import model.session.CNFUse;
import model.solver.SolverFactory;
import vue.IIG;
import vue.IIGSessionManager;

/**
 * Implementation of {@link IController}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class Controller implements IController {

	public static final String		DEFAULT_SOLVER_DIRECTORY		= "./solveur";
	public static final String		DEFAULT_PREPROCESSOR_DIRECTORY	= "./preproc";
	public static final String		DEFAULT_CNF_DIRECTORY			= "./cnf";

	public static final String		CLOSE_QUESTION					= "Do you really want to close the application ?\n";


	// private File solverDirectory = new File(DEFAULT_SOLVER_DIRECTORY);
	// private File preprocDirectory = new File(DEFAULT_PREPROCESSOR_DIRECTORY);
	private File					cnfDirectory					= new File(DEFAULT_CNF_DIRECTORY);

	private SolverFactory			solverFactory					= new SolverFactory(new File(DEFAULT_SOLVER_DIRECTORY));
	private PreProcessorFactory		preprocFactory					= new PreProcessorFactory(new File(DEFAULT_PREPROCESSOR_DIRECTORY));

	private IIG						ig;
	private List<SessionManager>	sessions						= new ArrayList<SessionManager>();

	/**
	 * 
	 */
	public Controller(IIG ig) {
		// TODO Auto-generated constructor stub
		this.ig = ig;
	}

	@Override
	public boolean changeDefaultCNFDirectory(File directory) {
		if (!directory.isDirectory())
			return false;

		cnfDirectory = directory;
		return true;
	}

	@Override
	public boolean changePreProcessorDirectory(File directory) {
		if (!directory.isDirectory())
			return false;

		preprocFactory.setDirectory(directory);
		return true;
	}

	@Override
	public boolean changeSolverDirectory(File directory) {
		if (!directory.isDirectory())
			return false;

		solverFactory.setDirectory(directory);
		return true;
	}

	@Override
	public void close() {
		// Iterator<SessionManager> ite = sessions.iterator();
		if (!ig.yesNoQuestionPopup(CLOSE_QUESTION))
			return;


		Set<SessionManager> tmp = new HashSet<SessionManager>(sessions);


		for (SessionManager s : tmp)
			closeSession(s);
		System.exit(0);
	}

	@Override
	public void closeSession(ISessionManager session) {
		// TODO Auto-generated method stub


		SessionManager ses = null;

		Iterator<SessionManager> it = sessions.iterator();
		while (it.hasNext())
		{
			ses = it.next();
			if (ses == session)
			{
				it.remove();
				break;
			}

			ses = null;
		}
		// sessions.remove(i);
		if (!ses.close())
			ig.showErrorPopup(EErrorPopUp.ERROR_CLOSE_SESSION, ses.toString());

		ig.closeSession(session);
		System.out.println("sesison supprimer");
	}

	@Override
	public void openSession(String cnf) {
		// TODO Auto-generated method stub

		CNF filecnf = null;
		try
		{
			filecnf = new FileCNF(cnf);
		} catch (Exception e)
		{
			e.printStackTrace();
			ig.showErrorPopup(EErrorPopUp.ERROR_OPEN_CNF, cnf);
			return;
		}

		CNFUse cnfuse = new CNFUse(filecnf);


		// ouvre session controller
		SessionManager man = new SessionManager(cnfuse, solverFactory, preprocFactory);

		IIGSessionManager igman = ig.openSession(cnfuse, man);
		// ouvre session ig
		if (igman == null)
		{// pb
			ig.showErrorPopup(EErrorPopUp.ERROR_OPEN_IG_SESSION, "");
			filecnf.close();
			man.close();
			return;
		}

		// maj
		man.setIIGSessionManager(igman);
		sessions.add(man);
		// igman.setSessionManager(man);

	}

	@Override
	public void openSession(File cnf) {
		// TODO Auto-generated method stub
		openSession(cnf.getPath());
	}

	@Override
	public File getDefaultCNFDirectory() {
		return cnfDirectory;
	}

	@Override
	public File getPreProcessorDirectory() {
		return preprocFactory.getDirectory();
	}

	@Override
	public File getSolverDirectory() {
		return solverFactory.getDirectory();
	}

}
